if(!neo4j_config){
    var neo4j_config = {
        useNeo4j:true,
        url:"bolt://"+window.location.hostname,
        user:"neo4j",
        password:"123456"
    };
}
class Neo4jService{
    constructor(){
        this.rebuild();
    }
    getSession(){
        if (neo4j_config.useNeo4j) {
            return this.driver.session();
        }
        return this.remoteSession;
    }

    closeConnection() {
        if (neo4j_config.useNeo4j) {
            this.driver.close();
        }
    };

    rebuild() {
        if (neo4j_config.useNeo4j) {
            this.driver = neo4j.v1.driver(neo4j_config.url, neo4j.v1.auth.basic(neo4j_config.user, neo4j_config.password));
        }else {
            this.remoteSession = new RemoteNeo4jServiceSession(neo4j_config);
        }
    };
}

class MyRecord {
    constructor(record) {
        this.keys = Object.keys(record);
        this.valueMap = record;
    }
    get(indexOrName){
        if (indexOrName in this.valueMap){
            return this.valueMap[indexOrName];
        }
        if (typeof indexOrName === 'number' && indexOrName%1===0 && indexOrName>=0 && indexOrName<this.keys.length ) {
            return this.valueMap[this.keys[indexOrName]];
        }
        return null;
    }
}

class RemoteNeo4jServiceSession {
    constructor(conf){
        this.config = Object.assign({},RemoteNeo4jServiceSession.default,conf);
    }
    run(cypher){
        let formData = new FormData();
        formData.append('cypher' , cypher);
        return fetch(this.config.base_url,{
            method:"POST",
            body:formData,
            // headers:{
            //     "Content-Type": "application/x-www-form-urlencoded"
            // }
        })
            .then(response=>response.json())
            .then(records=>{
                return {
                    records:records.map(r=>new MyRecord(r))
                }
            });
    }

    close(){

    }
}
RemoteNeo4jServiceSession.default = {
    base_url:'http://127.0.0.1:8080/cypher'
};

const neo4jService = new Neo4jService();